package com.bosssoft.goods.service.impl;

import com.bosssoft.base.entity.PaginationData;
import com.bosssoft.goods.dao.GoodsCategoryDao;
import com.bosssoft.goods.domain.vo.AddCategoryVO;
import com.bosssoft.goods.domain.vo.CategoryListVO;
import com.bosssoft.goods.domain.vo.EditCategoryVO;
import com.bosssoft.goods.handle.AddCategoryHandle;

import com.bosssoft.goods.service.GoodsCategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品分类服务接口类实现
 * @author Fang_Sir
 * @date 2021-04-12
 */
@Service
public class GoodsCategoryServiceImpl implements GoodsCategoryService {
    @Autowired
    private GoodsCategoryDao goodsCategoryDao;
    @Autowired
    private AddCategoryHandle addCategoryHandle;

    @Override
    public PaginationData<CategoryListVO> getCategoryList(int page, int size) {
        if(page > 0) {
            PageHelper.startPage(page, size);
            List<CategoryListVO> result = goodsCategoryDao.getCategoryListFromRoot();
            PageInfo<CategoryListVO> info = new PageInfo<>(result);
            return new PaginationData<>(page, info.getTotal(), info.getList());
        }else {
            List<CategoryListVO> result = goodsCategoryDao.getCategoryListFromRoot();
            return new PaginationData<>(page, result.size(), result);
        }
    }

    @Override
    public boolean addCategory(AddCategoryVO info) {
        return addCategoryHandle.addGoodCategory(info, 10000000);
    }

    @Override
    public boolean isCategoryExist(int level, String name) {
        switch (level) {
            case 0:
                return goodsCategoryDao.isExistCategoryRoot(name) > 0;
            case 1:
                return goodsCategoryDao.isExistCategoryFunction(name) > 0;
            case 2:
                return goodsCategoryDao.isExistCategoryType(name) > 0;
            default:
                return false;
        }
    }

    @Override
    public boolean editCategoryName(EditCategoryVO info) throws Exception {
        if(isCategoryExist(info.getLevel(), info.getName())) {
            throw new Exception("分类名称已存在！");
        }
        switch (info.getLevel()) {
            case 0:
                return goodsCategoryDao.editCategoryRoot(info.getId(),info.getName()) > 0;
            case 1:
                return goodsCategoryDao.editCategoryFunction(info.getId(),info.getName()) > 0;
            case 2:
                return goodsCategoryDao.editCategoryType(info.getId(),info.getName()) > 0;
            default:
                return false;
        }
    }

    @Override
    public boolean delCategory(int level, int id) {
        switch (level) {
            case 0:
                return goodsCategoryDao.delCategoryRoot(id) > 0;
            case 1:
                return goodsCategoryDao.delCategoryFunction(id) > 0;
            case 2:
                return goodsCategoryDao.delCategoryType(id) > 0;
            default:
                return false;
        }
    }

}
